package cn.edu.hizee.gps.dao;

import cn.edu.hizee.gps.model.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZX
 */
public interface UserInfoRepository extends JpaRepository<UserInfo,Integer>, CrudRepository<UserInfo,Integer> {

    /*@Query(nativeQuery = true, value = "SELECT " +
            "t_user_info.id, " +
            "t_user_info.user_id, " +
            "t_user_info.sfzno, " +
            "t_user_info.image_path, " +
            "t_user_info.age, " +
            "t_user_info.sex, " +
            "t_user_info.height, " +
            "t_user_info.blood " +
            "FROM " +
            "t_user_info " +
            "WHERE t_user_info.user_id = :userId")*/
    UserInfo findByUserId(@Param("user_id") Integer userId);

    /**
     *
     * @param sfz
     * @param height
     * @param blood
     * @param sex
     * @param id
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true,value = "UPDATE t_user_info  " +
            "SET t_user_info.sfzno = :sfz, " +
            "t_user_info.sex = :sex, " +
            "t_user_info.height = :height, " +
            "t_user_info.blood = :blood "+
            "WHERE " +
            "t_user_info.user_id = :id")
    void updateByUserId(@Param("sfz") String sfz, @Param("height") String height, @Param("blood") String blood, @Param("sex") String sex, @Param("id") Integer id);

    /*
        用户信息
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "t_user.id, " +
            "t_user.real_name, " +
            "t_user.nickname, " +
            "t_user.phone, " +
            "t_user_info.image_path, " +
            "t_user_info.age, " +
            "t_user_info.sex, " +
            "t_user_info.height, " +
            "t_user_info.blood, " +
            "t_user_info.illness " +
            "FROM " +
            "t_user " +
            "JOIN t_user_info on t_user_info.user_id = t_user.id " +
            "WHERE t_user.id = :userId ")
    List<Map> findUserInfo(@Param("userId") int userId);
}
